<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>后台管理</title>
    <link rel="icon" href="${pageContext.request.contextPath}/lib/static/login/images/favicon.ico">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/lib/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/lib/static/common/js/jquery.min.js" charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/lib/layui/layui.js" charset="utf-8"></script>
</head>
<body>
<div class="layui-fluid">
    <hr class="layui-bg-red">

    <form class="layui-form" >
        <div class="layui-form-item layui-hide">
            <label class="layui-form-label">ID</label>
            <div class="layui-input-block">
                <input type="text" name="id"   placeholder="0" checked>
            </div>
        </div>
        <div class="layui-form-item">
            <input type="hidden" id="type">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
               <input type="radio" name="type" id="radio1" value="0" title="通知">
               <input type="radio" name="type" value="1" title="公告" >
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">发布内容</label>
            <div class="layui-input-block">
                <textarea name="context" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">发布者</label>
            <div class="layui-input-block">
                <input type="text" name="publisher" required  lay-verify="required" placeholder="请输入发布人" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <input type="hidden" name="department">
            <label class="layui-form-label">部门</label>
            <div class="layui-input-block">
                <select id="department" name="department" required lay-verType="tips" lay-verify="department" lay-filter="selectDept" lay-search>
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <input type="hidden" id="status">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="0" title="一般">
                <input type="radio" name="status" value="1" title="重要" >
                <input type="radio" name="status" value="2" title="紧急" >
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="saveNotice">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
<script>
    var baseUrl = '${pageContext.request.contextPath}/notice/';


    /*发送ajax请求访问后台*/
    function sendAjaxPostReq($, param, method) {
        $.post(
            baseUrl + method,
            param,
            function (result) {
                if (result.code == 0) {
                    layer.msg(result.msg, {icon: 1, time: 500}, function () {
                        parent.window.location.reload();
                    });
                } else {
                    layer.msg(result.msg, {icon: 5, anim: 6});
                }
            },
            "json"
        );
    }

    layui.use(['form', 'jquery'], function () {
        var form = layui.form;
        var $ = layui.$;

        $(function () {
            $("input[type='radio'][name='type']").eq($("#type").val()).attr("checked", true).siblings().attr("checked", false);
            $("input[type='radio'][name='status']").eq($("#status").val()).attr("checked", true).siblings().attr("checked", false);
            form.render();
            return false;
        });

        $(function () {
            /*加载部门信息*/
            showDeptSelectInfo();
        });
        /*加载部门信息*/
        function showDeptSelectInfo() {
            var department = $("body").find("input[name='department']").val();
            $.post(
                baseUrl + "getDeptIdAndName",
                function (result) {
                    var dept = result.data;
                    $.each(dept, function (index, dept) {
                        /*修改时选中状态*/
                        if (dept.deptName == department) {
                            $("#department").append("<option selected checked value='" + dept.deptName + "'>" + dept.deptName + "</option>");
                        } else {
                            $("#department").append("<option value='" + dept.deptName + "'>" + dept.deptName + "</option>");
                        }
                    });
                    form.render('select');
                    return false;
                },
                "json"
            );
        }

        //自定义验证规则
        form.verify({
            title: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '类型名不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '类型名首尾不能出现下划线\'_\'';
                }
                if (/^\d+\d+\d$/.test(value)) {
                    return '类型名不能全为数字';
                }
            },
            //password: [/(.+){6,12}$/, '密码必须6到12位']
        });
        /*表单提交 修改*/
        form.on('submit(edit)', function (data) {
            sendAjaxPostReq($, data.field, "updateNotice");
            return false;
        });
        /*表单提交*/
        form.on('submit(saveNotice)', function (data) {
            sendAjaxPostReq($, data.field, "add");
            return false;
        });
    });

    //Demo
    /*layui.use('form', function(){
        var form = layui.form;
        //提交添加
        form.on('submit(formDemo)', function(data){
            var grant = data.field.switch;
            var grantEnable;
            if(grant === 'on'){
                grantEnable = '已授权'
            }else{
                grantEnable = '未授权'
            }
            var params={
                 id:data.field.id
                ,type:data.field.type
                ,title:data.field.title
                ,context:data.field.context
                ,publisher:data.field.publisher
                ,department:data.field.department
                ,status:data.field.status

            }
            //发起请求
            $.post(url,params,function (result) {
                //假设这是iframe页
                //先得到当前iframe层的索引
                var index = parent.layer.getFrameIndex(window.name);
                //在执行关闭
                parent.layer.close(index);
            });
            return false;
        });
    });*/


</script>
</div>
</body>
</html>
